package com.heima.wemedia.delay;

import com.heima.common.delayTask.RedisDelayedQueueUtil;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.feign.ArticleFeignClient;
import com.heima.model.media.dtos.WmNewsDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import com.heima.wemedia.entity.WmNews;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.TimerTask;
import java.util.concurrent.*;

@Component
@Slf4j
public class RedisDelayedQueueInit {

    @Autowired
    private RedisDelayedQueueUtil redisDelayedQueueUtil;

    @Autowired
    private ArticleFeignClient articleFeignClient;

    @Autowired
    private WmNewsService wmNewsService;

    @PostConstruct
    public void run(){
        ExecutorService executorService = Executors.newCachedThreadPool();
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        scheduledExecutorService.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                try {
                    Object obj = redisDelayedQueueUtil.getDelayQueue("wm.news.pub");
                    if (obj != null) {
                        WmNews wmNews = (WmNews) obj;
                        executorService.submit(new Runnable() {
                            @Override
                            public void run() {
                                wmNews.setStatus(9);
                                WmNewsResultDTO dto = BeanHelper.copyProperties(wmNews, WmNewsResultDTO.class);
                                Long articleId = articleFeignClient.save(dto);
                                wmNews.setArticleId(articleId);
                                wmNewsService.updateById(wmNews);
                            }
                        });
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, 0, 1,TimeUnit.SECONDS);
        log.info("监听延迟队列成功");
        redisDelayedQueueUtil.addQueue(null, 1, TimeUnit.SECONDS, "wm.news.pub");
    }

}
/*
@Slf4j
@Component
public class RedisDelayedQueueInit {

    @Autowired
    private RedisDelayedQueueUtil redisDelayedQueueUtil;

    @Autowired
    private ArticleFeignClient articleFeign;

    @Autowired
    private WmNewsService wmNewsService;

    @PostConstruct
    public void run() throws Exception {
        //线程池
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        //周期运行的线程池
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        executorService.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                try {
                    Object obj = redisDelayedQueueUtil.getDelayQueue("wm.news.pub");
                    Integer wmNewsId = Integer.valueOf(obj.toString());
                    log.info("获取到发布文章id，wmNewsId={}",wmNewsId);
                    //        调用发布方法
                    if(wmNewsId != 0){
                        cachedThreadPool.submit(new Runnable() {
                            @Override
                            public void run() {
                                WmNews wmNews = new WmNews();
                                wmNews.setId(wmNewsId);
                                publishWmNews(wmNews,2);
                            }
                        });
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },0,1, TimeUnit.SECONDS);
        log.info("延迟队列监听成功！！");

        //        初始化完成后，向队列添加一个无用的元素
        redisDelayedQueueUtil.addQueue(0,1, TimeUnit.SECONDS,"wm.news.pub");
    }
    public void publishWmNews(WmNews wmNews,int source) {
        Integer wmNewsId = wmNews.getId();
        if(source ==2){
            wmNews =  wmNewsService.getById(wmNewsId);
        }
        WmNewsResultDTO newsResultDTO = BeanHelper.copyProperties(wmNews, WmNewsResultDTO.class);
//        如果要发布，远程调用article服务，保存文章
        try{
            Long apAritlceId = articleFeign.save(newsResultDTO);
//        修改文章状态-已发布-9
            wmNews.setStatus(9);
            wmNews.setArticleId(apAritlceId);
            wmNewsService.updateById(wmNews);
        }catch (Exception e){
            log.error("远程调用article发布文章失败！");
            e.printStackTrace();
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
    }
}*/
